package cn.bluemobi.dylan.step.presenter.login;

import android.os.Handler;

import javax.inject.Inject;

import cn.bluemobi.dylan.step.base.RxPresenter;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.moder.http.CommonSubscriber;
import cn.bluemobi.dylan.step.moder.http.RetrofitHelper;
import cn.bluemobi.dylan.step.moder.http.RxUtil;
import cn.bluemobi.dylan.step.moder.http.response.HttpResponseBean;
import cn.bluemobi.dylan.step.moder.prefs.Prefs;
import cn.bluemobi.dylan.step.moder.prefs.SharedPreferencesHelper;
import cn.bluemobi.dylan.step.presenter.contract.login.LoginContract;

/**
 * Created by $Liudan on 2018/8/7 0007.
 */
public class LoginPresenter extends RxPresenter<LoginContract.View> implements LoginContract.Presenter {
    @Inject
    SharedPreferencesHelper prefHelper;

    @Inject
    public LoginPresenter() {

    }

    /**
     * 登录
     */
    public void login(String memberPhone,String phoneCode){
        addSubscribe(RetrofitHelper.getLoginApi().login(memberPhone, phoneCode)
                        .compose(RxUtil.<HttpResponseBean<User>>rxSchedulerHelper())
                        .compose(RxUtil.<User>handleResult())
                        .subscribeWith(new CommonSubscriber<User>(mView) {
                            @Override
                            public void onNext(User user) {
                                prefHelper.setPrefObject(Prefs.key_user,user);
                                mView.httpLoginSuccess(user);
                            }
                        })
        );
    }

    /**
     * 获取验证码
     */
    public void httpGetCode(String memberPhone){
        addSubscribe(RetrofitHelper.getLoginApi().getPhoneCode(memberPhone)
                .compose(RxUtil.<HttpResponseBean<User>>rxSchedulerHelper())
                .compose(RxUtil.<User>handleResult())
                .subscribeWith(new CommonSubscriber<User>(mView) {
                    @Override
                    public void onNext(User user) {
                        mView.httpRequestCodeSuccess(user);
                    }
                })
        );
    }


//    private User getUser() {
//        return prefHelper.getPrefObject(Prefs.key_user, User.class);
//    }
}
